<template>
  <div>
     <SelfAdaption slot="adaption-bar">
      <Form :model="AddressGroupsearchForm" ref="formSearch" slot="adaption-bar" class="ivu-form-no-margin-bottom" inline>
        <Form-item prop='code' label="收件人组名：">
            <Input
                type="text"
                placeholder="请输入收件人组名"
                v-model="AddressGroupsearchForm.groupName"
            ></Input>
        </Form-item>
        <Form-item prop="locked" label="收件人：">
            <Input
              type="text"
              placeholder="请输入收件人"
              v-model="AddressGroupsearchForm.receiver"
            ></Input>
        </Form-item>
         <Form-item   prop="locked" label="单位名称：">
            <Select
            filterable
            style="width: 100%;"
            clearable
            placeholder="请选择单位名称"
            v-model="AddressGroupsearchForm.trgNode"
            >
              <Option v-for="item in bussinessUnitList" :value="item.value" :key="item.value">{{item.value}}</Option>
            </Select>
        </Form-item>
        <Form-item prop="validFlag" :label="'状态：'">
          <Select
            style="width: 100%"
            clearable
            placeholder="请选择状态"
            v-model="AddressGroupsearchForm.validFlag"
          >
            <Option v-for="item in addressGroupStatusList" :value="item.value" :key="item.value">{{ item.text }}</Option>
          </Select>
        </Form-item>
        <!--  查询 -->
        <Form-item prop="button" class="search-solt-btn">
          <RPButton optType="search" ref='search' @click="searchAddressGroupValue" />
        </Form-item>
      </Form>
    </SelfAdaption>
    <InnerPageTable ref="AddressGroupTable" :config="pageTableConfig" v-if='showTable'>
      <div slot="table-bar">
        <RPButton  optType="create" type="info" @click="add" />
      </div>
    </InnerPageTable>
  </div>
</template>

<script>
import { AddressGroupLists, columnListAddressGroup } from './columns.js'
import addressGroupApi from '@/api/addressGroup.js'
import coverApi from '@/api/cover.js'
import AddressGroupEdit from './AddressGroupEdit.vue'
import tableColumnSetting from '@/mixin/tableColumnSetting'
import {
  ADDRESS_GROUP_STATE_LIST,
  ADDRESS_GROUP_STATE_EFFECTIVE,
  ADDRESS_GROUP_STATE_DRAFT
} from './const.js'
export default {
    mixins: [tableColumnSetting],
    data(){
      return{
        pageTableConfig:{
          loadDataFn: this.getAddressGroupList,
          columns:  AddressGroupLists.bind(this)(),
          autoFirst: false
        },
        AddressGroupsearchForm: {
            groupName: '',
            receiver: '',
            trgNode: '',
            validFlag: '',
            reportCode: '',
            reportTypeCode: ''
          },
        bussinessUnitList:[],
        menuNo: 'addressGroup',
        columnList: columnListAddressGroup,
        addressGroupStatusList: ADDRESS_GROUP_STATE_LIST,
      }
    },
    methods:{
      getBussinessUnitList() {
        coverApi.getRptTrgnodeTree({parentNodeCode: -1}).then(res => {
          let data = res.data.data
          let result = []
          if (data && data.length > 0) {
            data.forEach(item => {
              result.push({
                text: item.nodeName,
                value: item.nodeName,
                nodeId: item.nodeId
              })
            })
          }
          this.bussinessUnitList = result
        })
      },
      searchAddressGroupValue(){
        this.$refs['AddressGroupTable'].load(this.AddressGroupsearchForm)
      },
      getAddressGroupList: tableObj => {
        let { responseProcess, param } = tableObj
        let { pageNum, pageSize, groupName, receiver, trgNode, validFlag, reportCode,
            reportTypeCode} = param
        addressGroupApi.getTableList({
          groupName,
          receiver,
          trgNode,
          validFlag,
          currentPage: pageNum,
          pageSize,
          reportCode,
          reportTypeCode
          // sortOrderBy: "DESC",
          // sortPropName: "create_time"
        }).then(res => {
        let { current, size, records, pages, total  } = res.data.data
          responseProcess({
            pageNum: current,
            size,
            pages,
            records: records || [],
            total,
          });
        })
      },
      add(){
        new this.$sidelipPage(
          AddressGroupEdit,
          {
          props:{
            title:'新建收件人组',
            bussinessUnitList: this.bussinessUnitList
          }
          },
            data => {
            data && this.searchAddressGroupValue()
          }
      );
     },
    goAddressGroupEdit(row){
       new this.$sidelipPage(
          AddressGroupEdit,
          {
          props:{
            title:'编辑收件人组',
            id: row.id,
            bussinessUnitList: this.bussinessUnitList
          }
          },
            data => {
            data && this.searchAddressGroupValue()
          }
      );
    },
    goAddressGroupSubmit(row) {
      let params = {
        id: '',
        groupName:'',
        trgNode: '',
        trgNodeNodeId: '',
        company: '',
        companyNodeId: '',
        receiver:'',
        ccs:''
      }
      let temp = {}
      Object.keys(params).forEach(key => {
          temp[key] = row[key]
      })
      params = temp
      params.validFlag = ADDRESS_GROUP_STATE_EFFECTIVE
      addressGroupApi.editInfo(params).then(() => {
        this.$Message.success({
          content: '提交成功',
          duration: 3
        })
        this.searchAddressGroupValue()
      })
    },
    goAddressGroupDetail(row){
        new this.$sidelipPage(
          AddressGroupEdit,
          {
          props:{
            title:'收件人组详情',
            id: row.id,
            action: 'detail',
            bussinessUnitList: this.bussinessUnitList,
            disabledPage: true,
          }
          },
            data => {
            data && this.searchAddressGroupValue()
          }
      );
    },
    deleteAddressGroup(row){
      let msg = ''
      if (row.validFlag == ADDRESS_GROUP_STATE_EFFECTIVE) {
        msg = '删除后将导致关联该收件人组的提醒规则组发送邮件失败，你确认要删除吗？'
      } else if (row.validFlag == ADDRESS_GROUP_STATE_DRAFT) {
        msg = '删除后不可撤回，您确认要删除吗？'
      }
      this.$confirm({
        title:"收件人删除",
        content: msg
      }).then(()=>{
        addressGroupApi.deleteInfo({
          data: [row.id]
        }).then(() => {
          this.$Message.success({
            content: '删除成功',
            duration: 3
          })
          this.searchAddressGroupValue()
        })
      })
    },
      init() {
        this.getBussinessUnitList()
        this.searchAddressGroupValue()
      }
    },
    crested(){

    }
}
</script>

<style lang="less" scoped>
/deep/.ivu-select-small .ivu-select-input, .ivu-select-small.ivu-select-multiple .ivu-select-input {
    height: 32px;
    line-height: 32px;
    top: 0;
}
</style>
